package com.mineapp.weight

/**
 * Created by cyril on 16/7/11.
 */

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator

/**
 * ----------Dragon be here!----------/
 * ***┏┓******┏┓*********
 * *┏━┛┻━━━━━━┛┻━━┓*******
 * *┃             ┃*******
 * *┃     ━━━     ┃*******
 * *┃             ┃*******
 * *┃  ━┳┛   ┗┳━  ┃*******
 * *┃             ┃*******
 * *┃     ━┻━     ┃*******
 * *┃             ┃*******
 * *┗━━━┓     ┏━━━┛*******
 * *****┃     ┃神兽保佑*****
 * *****┃     ┃代码无BUG！***
 * *****┃     ┗━━━━━━━━┓*****
 * *****┃              ┣┓****
 * *****┃              ┏┛****
 * *****┗━┓┓┏━━━━┳┓┏━━━┛*****
 * *******┃┫┫****┃┫┫********
 * *******┗┻┛****┗┻┛*********
 * ━━━━━━神兽出没━━━━━━by:wangziren
 */


class EatBeanLoadingView(context: Context, attrs: AttributeSet) : View(context, attrs) {
    private var mPaint: Paint? = null
    private var mPaintEye: Paint? = null

    private var mWidth = 0f
    private var mHeight = 0f
    private val mPadding = 5f

    private val eatErWidth = 60f
    private var eatErPositionX = 0f
    internal var eatSpeed = 5
    private val beansWidth = 10f

    private val mAngle = 34f
    private var eatErStartAngle = mAngle
    private val eatErEndAngle = 360 - 2 * eatErStartAngle

    init {
        initPaint()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = measuredWidth.toFloat()
        mHeight = measuredHeight.toFloat()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val eatRightX = mPadding + eatErWidth + eatErPositionX
        val rectF = RectF(mPadding + eatErPositionX, mHeight / 2 - eatErWidth / 2,
                eatRightX, mHeight / 2 + eatErWidth / 2)
        canvas.drawArc(rectF, eatErStartAngle, eatErEndAngle, true, mPaint!!)
        canvas.drawCircle(mPadding + eatErPositionX + eatErWidth / 2, mHeight / 2 - eatErWidth / 4,
                beansWidth / 2, mPaintEye!!)

        val beansCount = ((mWidth - mPadding * 2 - eatErWidth) / beansWidth / 2f).toInt()
        for (i in 0..beansCount - 1) {
            val x = (beansCount * i).toFloat() + beansWidth / 2 + mPadding + eatErWidth
            if (x > eatRightX) {
                canvas.drawCircle(x, mHeight / 2, beansWidth / 2, mPaint!!)
            }
        }
    }

    private fun initPaint() {
        mPaint = Paint()
        mPaint!!.isAntiAlias = true
        mPaint!!.style = Paint.Style.FILL
        mPaint!!.color = Color.WHITE

        mPaintEye = Paint()
        mPaintEye!!.isAntiAlias = true
        mPaintEye!!.style = Paint.Style.FILL
        mPaintEye!!.color = Color.BLACK
    }

    internal var valueAnimator: ValueAnimator? = null

    fun stopAnim() {
        if (valueAnimator != null) {
            clearAnimation()
            valueAnimator!!.repeatCount = 0
            valueAnimator!!.cancel()
            valueAnimator!!.end()
            eatErPositionX = 0f
            postInvalidate()
        }
    }

    private fun startViewAnim(startF: Float, endF: Float, time: Long): ValueAnimator? {
        valueAnimator = ValueAnimator.ofFloat(startF, endF)
        valueAnimator!!.duration = time
        valueAnimator!!.interpolator = LinearInterpolator()
        valueAnimator!!.repeatCount = ValueAnimator.INFINITE
        valueAnimator!!.repeatMode = ValueAnimator.RESTART
        valueAnimator!!.addUpdateListener {
            val mAnimatedValue = valueAnimator!!.animatedValue as Float
            eatErPositionX = (mWidth - 2 * mPadding - eatErWidth) * mAnimatedValue
            eatErStartAngle = mAngle * (1 - (mAnimatedValue * eatSpeed - (mAnimatedValue * eatSpeed).toInt()))
            invalidate()
        }

        valueAnimator!!.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
            }

            override fun onAnimationRepeat(animation: Animator) {
                super.onAnimationRepeat(animation)
            }

            override fun onAnimationStart(animation: Animator) {
                super.onAnimationStart(animation)
            }
        })
        if (!valueAnimator!!.isRunning) {
            valueAnimator!!.start()
        }
        return valueAnimator
    }

    fun startAnim() {
        stopAnim()
        startViewAnim(0f, 1f, 3500)
    }

}
